const Stomp=require("stomp-client");
const mongoose=require("mongoose")

var Merchant=mongoose.model("merchant")
var Food=mongoose.model("food")
var Order=mongoose.model("order")

var config=require("./config.js")

// var regDest="/queue/ita_p4_reg_mq";
// var foodDest="/queue/ita_p4_pic_mq";
// var compDest="/queue/ita_p4_complain_mq";
// var scoreDest="/queue/ita_p4_score_mq";

var client=new Stomp(config.ACtiveMQIP,61613, "", "");

client.connect(function(sessionID){
	//用户注册
	client.subscribe(config.regDest,function(body,headers){
		var b=JSON.parse(body)
		var m=new Merchant()
		m._id=b.mId;
		m.mPersonName=b.mPersonName;
		m.mTel=b.mTel;
		m.mIdCard=b.mIdCard;
		m.mCardPath=b.mCardPath;
		m.mLocation=b.mLocation;
		m.mBrand=b.mBrand;
		m.mStatus=1;
		m.mScore=10;
		m.save(function(err){
			if(!err){
				console.log("merchant save success")
			}else{
				console.log("merchant save fail")
			}
		})
	})
	
	//推荐菜品
	client.subscribe(config.foodDest,function(body,headers){

		var b=JSON.parse(body)
		var f=new Food()
		f._id=b.fId;
		f.fName=b.fName;
		f.mId=b.mId;
		f.fViewPath=b.fViewPath;
		f.fStatus=b.fStatus;
		f.save(function(err){
			if(!err){
				console.log("food save success")
			}else{
				console.log("food save fail")
			}
		})
	})

	//投诉订单
	client.subscribe(config.compDest,function(body,headers){
		// console.log(body)
		var o=new Order();
		// console.log("-----------------")
		o._id=body.split("#")[0];
		o.mBrand=body.split("#")[1];
		o.oStatus=1;
		// console.log(o)
		// console.log("-----------------")
		o.save(function(err){
			if(!err){
				console.log("order save success")
			}else{
				console.log("order save fail")
			}
		})
	})
	
	//评分
	client.subscribe(config.scoreDest,function(body,headers){
		Merchant.findByIdAndUpdate(body.split("#")[0],{mScore:body.split("#")[1]},function(err,oldMerchant){
			if(!err){
				console.log("merchant update success")
			}else{
				console.log("merchant update fail")
			}
		})
	})

})